package com.infovalley.info_valley.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.infovalley.tx_plugin.PluginConstants;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;

/**
 * 微信支付后的回调
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI iwxapi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        iwxapi = PluginConstants.getWxApi();
        iwxapi.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        iwxapi.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {
        Log.d("Nick", "wx pay request");
    }

    @Override
    public void onResp(BaseResp baseResp) {

        // 0 支付成功
        //-1 发生错误 可能的原因 签名错误、未注册AppID、项目设置AppID不正确、
        // 注册的AppID与设置的不匹配、其他异常等
        //-2 用户取消 发生场景 用户不支付了,点击取消,返回APP
        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {

            Intent intent = new Intent(PluginConstants.WX_PAY_FILTER);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("errCode", baseResp.errCode);
            intent.putExtra("errStr", baseResp.errStr);
            intent.putExtra("transaction", baseResp.transaction);
            intent.putExtra("openId", baseResp.openId);

            Log.d("Nick", "WXPayEntryActivity errCode =:" + baseResp.errCode);
            Log.d("Nick", "WXPayEntryActivity---errStr:" + baseResp.errStr);
            Log.d("Nick", "WXPayEntryActivity---transaction:" + baseResp.transaction);
            Log.d("Nick", "WXPayEntryActivity---openId:" + baseResp.openId);
            sendBroadcast(intent);
        }
        finish();
    }
}
